<?php
/**
 * Created by PhpStorm.
 * User: Min
 * Date: 2018/1/24
 * Time: 17:36
 */

namespace App\Services\Goods;

use Packages\OpenSearch\CloudsearchClient;
use Packages\OpenSearch\CloudsearchSearch;

class OpenSearchService {

    /**
     * @var int
     */
    public $wid = 8;
    /**
     * @var
     */
    public $origin;
    /**
     * @var
     */
    public $brandId;
    /**
     * @var
     */
    public $catId;
    /**
     * @var CloudsearchClient
     */
    protected $client;
    /**
     * @var int
     */
    protected $pageSize = 15;
    /**
     * @var string
     */
    protected $appName = 'sdx';

    /**
     * OpenSearchService constructor.
     */
    public function __construct() {

        $opts = $this->getOpts();

        $this->client = new CloudsearchClient(config('ali.ALIYUN_AKID'), config('ali.ALIYUN_AKSECRET'), $opts, "aliyun");

    }

    /**
     * @author Min
     * @return array
     */
    protected function getOpts()
    {
        if(in_array($_SERVER['SERVER_NAME'], ['api.shidexian.com', 'api.test.shidexian.com'])) {
            $hots = "http://intranet.opensearch-cn-hangzhou.aliyuncs.com";
        } else {
            $hots = "http://opensearch-cn-hangzhou.aliyuncs.com";
        }

        return ['host' => $hots];
    }

    /**
     * 获取商品搜索结果
     * @author Min
     * @param $keyword
     * @param int $start_offset
     * @param array $sort
     * @return mixed
     */
    public function getSearchList($keyword, $start_offset=0, $sort=array()){
        $search = new CloudsearchSearch($this->client);
        $search->addIndex($this->appName);
        $search->setHits($this->pageSize);
        $search->setStartHit($start_offset);
        $search->setQueryString("default:'$keyword'");
        $search->setFormulaName("text_relevance_test");
        $search->setFormat("json");
        $search->addFilter('is_on_sale=1');
        $search->addFilter('is_alone_sale=1');
        $search->addFilter('is_delete=0');
        $search->addFilter('wid='.$this->wid);
        $search->addFetchFields('goods_id;goods_name');
        $search->addDistinct('goods_id', 1, 1, 'false', '', 'true');

        if($sort){
            $search->addSort($sort['field'], $sort['sort']);
        }
        if($this->brandId){
            $search->addFilter('contain(brand_id, "'.$this->brandId.'")');
        }
        if($this->origin){
            $search->addFilter('contain(origin, "'.$this->origin.'")');
        }

        if($this->catId){
            $search->addFilter('contain(cat_id, "'.$this->catId.'")');
        }
        $json = $search->search();
        $result = json_decode($json,true);
        
        return $result['result'];
    }

    /**
     * 搜索类型筛选
     * @author Min
     * @param $keyword
     * @param $key
     * @param string $catId_list
     */
    public function getSearchType($keyword, $key, $catId_list=''){
        $search = new CloudsearchSearch($this->client);
        $search->addIndex($this->appName);
        $search->setHits(500);
        $search->setQueryString("default:'$keyword'");
        $search->addFilter('is_on_sale=1');
        $search->addFilter('is_alone_sale=1');
        $search->addFilter('is_delete=0');
        $search->addFilter('brand_id!=0');
        $search->addFilter('wid='.$this->wid);
        $search->addFetchFields('brand_id;brand_name;origin;cat_id');

        switch ($key){
            case 'get_brand':
                if($this->catId){
                    $search->addFilter('contain(cat_id, "'.$this->catId.'")');
                }
                if($this->origin){
                    $search->addFilter('contain(origin, "'.$this->origin.'")');
                }
                $search->addSort("brand_id", '+');
                $search->addDistinct('brand_id', 1, 1, 'false', '', 'true');
                break;

            case 'get_origin':
                if($this->catId){
                    $search->addFilter('contain(cat_id, "'.$this->catId.'")');
                }
                if($this->brandId){
                    $search->addFilter('contain(brand_id, "'.$this->brandId.'")');
                }
                $search->addDistinct('origin', 1, 1, 'false', '', 'true');
                break;

            case 'get_son_category':
                if($this->brandId){
                    $search->addFilter('contain(brand_id, "'.$this->brandId.'")');
                }
                if($this->origin){
                    $search->addFilter('contain(origin, "'.$this->origin.'")');
                }
                if($catId_list){
                    $search->addFilter('contain(cat_id, "'.$catId_list.'")');
                }
                $search->addDistinct('cat_id', 1, 1, 'false', '', 'true');
                break;

            case 'get_category':
                if($catId_list && $keyword==''){
                    $search->addFilter('contain(cat_id, "'.$catId_list.'")');
                }
                $search->addDistinct('cat_id', 1, 1, 'false', '', 'true');
                break;

            case 'get_category_parent':
                $search->addDistinct('cat_id_1', 1, 1, 'false', '', 'true');
                break;

            default:
                return;
        }
        $search->setFormat("json");
        $json = $search->search();

        $result = json_decode($json,true);
        return $result['result'];
    }

    /**
     * 返回下拉提示词
     * @author Min
     * @param $keyword
     * @return mixed
     */
    public function getSearchKeyword($keyword){
        $search = new CloudsearchSearch($this->client);
        $search->addIndex('keyword_pulldown');
        $search->setHits(10);
        $search->setQueryString("keyword:'$keyword'");
        $search->addSort("num", '-');
        $search->setFormat("json");

        $json = $search->search();
        $result = json_decode($json,true);

        return $result['result']['items'];
    }

    /**
     * 获取搜索品牌
     * @author Min
     * @param $keyword
     * @return array
     */
    public function getBrand($keyword){
        $brand_list = [];
        $data = $this->getSearchType($keyword, 'get_brand');

        foreach ($data['items'] as $key=>$value){
            $brand_list[$key]['id'] = $value['brand_id'];
            $brand_list[$key]['brand_name'] = $value['brand_name'];
        }

        return $brand_list;
    }

    /**
     * 获取搜索产地
     * @author Min
     * @param $keyword
     * @return array
     */
    public function getOrigin($keyword){
        $origin_list = array();
        $data = $this->getSearchType($keyword, 'get_origin');

        foreach ($data['items'] as $key=>$value){
            if($value['origin']){
                $origin_list[] = $value['origin'];
            }
        }

        rsort($origin_list);
        return $origin_list;
    }

    /**
     * 获取搜索分类id
     * @author Min
     * @param $keyword
     * @param string $catId_list
     * @param string $key
     * @return array
     */
    public function getCategoryId($keyword, $catId_list='', $key='get_category'){
        $category_id = array();
        $data = $this->getSearchType($keyword, $key, $catId_list);

        foreach ($data['items'] as $key=>$value){
            $category_id[] = $value['cat_id'];
        }

        return $category_id;
    }


}